<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<fmt:setBundle basename="messages"/>
<script type="text/javascript" language="javascript">
	var validSubmit = true;
	
	function submitAddNewCostElementTransport(form){
		var transportIdentifier = document.getElementById('transportId').value;
		var validCostElement = true;
		var transportAmount = "";
		
		<c:forEach items="${costElementView.transports}" var="costElementTransport">
			if (<c:out value="${costElementTransport.transportIdentifier}"/> == transportIdentifier){
				validCostElement = false;
				alert('<fmt:message key="warning.message.costelement.transport.alreadyAssigned"/>');
			}
		</c:forEach>
				
		if (validSubmit){
			if ((validCostElement)){
				transportAmount = form.transportAmount.value;
				transportAmount = transportAmount.replace(/,/g, "");
				
				if (isNaN(parseFloat(transportAmount)) || parseFloat(transportAmount) == 0.0 || parseFloat(transportAmount) > 99999.99){
					alert('<fmt:message key="warning.message.costelement.transport.invalid.amount"/>');
				} else {
					validSubmit = false;
					form.submit();
				}
			}
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}

	function submitBack(form){
		if (validSubmit){
			form.action = '<c:out value="${backLink}"/>';
			validSubmit = false;
			
			form.submit();
		} else {
			alert('<fmt:message key="warning.message.common.processing"/>');
		}
	}
</script>
<form name="CostElementTransportForm" action="addCostElementTransport.do" method="POST">
	<input type="hidden" id="ceId" name="costElementView.costElementID" value="<c:out value="${CostElementForm.costElementView.costElementID}"/>"/>
	<input type="hidden" id="ceCode" name="costElementView.costElementCode" value="<c:out value="${CostElementForm.costElementView.costElementCode}"/>"/>
	<input type="hidden" id="ceDescription" name="costElementView.costElementDescription" value="<c:out value="${CostElementForm.costElementView.costElementDescription}"/>"/>

	<table width="100%" align="center" border="0">
		<tr>
			<td style="width: 100%" align="center">
				<table style="width: 340" align="center" border="0" cellspacing="1" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna">
							<fmt:message key="label.costelements.transports.addTransport"/>
						</th>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.costelement.list.code"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" value="<c:out value="${CostElementForm.costElementView.costElementCode}"/>" onkeydown="return (numeric(event, 0) && noPaste(event, this));" size="15" maxlength="20" class="campo_texto" readonly="readonly" style="text-transform: uppercase;"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.costelement.list.description"/>
						</td>
						<td class="etiqueta_campo">
							<select name="transportView.transportIdentifier" id="transportId" class="campo_texto" style="width: 100%">
								<c:forEach items="${transportsList}" var="transportView">
									<option value="<c:out value="${transportView.transportIdentifier}"/>"><c:out value="${transportView.transportShortName}"/></option>
								</c:forEach>							
							</select>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.transports.costelements.amount"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" id="transportAmount" name="transportView.transportCost" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,4,false);" size="15" maxlength="10" class="campo_texto_r"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td height="10">
			</td>
		</tr>
		<tr>
			<td width="100%">
				<table align="center" width="340" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<td style="text-align: center; vertical-align: middle; width: 50%" class="columnadatoPar">
						<input type="button" value="<fmt:message key="label.common.add"/>" onclick="submitAddNewCostElementTransport(CostElementTransportForm);" class="boton"/>
							<input type="button" onclick="submitBack(CostElementTransportForm);" value="<fmt:message key="label.common.cancel"/>" class="boton"/>&nbsp;
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
